﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
/*
    2012-2-17 David检查正常
 */
namespace Benefit.Service.Sys
{
    /// <summary>
    /// 操作员每日的账户对应关系
    /// </summary>
    public class ServiceDayOperatorAccount : Benefit.Interface.Sys.IDayOperatorAccount
    {
        Benefit.DB.DBManager db = null;
        public ServiceDayOperatorAccount(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 初始化交易员账号关系
        /// </summary>
        /// <param name="_pdate"></param>
        public void InitDayOperatorAccount(DateTime _pdate)
        {
            Models.Sys.OperatorAccount oa = new Models.Sys.OperatorAccount();
            var query = from t in db.OperatorAccount.Include(a => a.Account).Include(a => a.Operator) select t;
            foreach (Models.Sys.OperatorAccount o in query)
            {
                if (!o.Account.IsDeleted && !o.Operator.IsDeleted)
                {
                    Models.Sys.DayOperatorAccount dgop = new Models.Sys.DayOperatorAccount();
                    dgop.Pdate = _pdate;
                    dgop.AccountId = o.AccountId;
                    dgop.OperatorId = o.OperatorId;
                    db.DayOperatorAccounts.Add(dgop);
                }
            }

            db.SaveChanges();
        }
        /// <summary>
        /// 删除操作员账号关系
        /// </summary>
        /// <param name="_pdate"></param>
        public void DeleteDayOperatorAccount(DateTime _pdate)
        {
            Models.Sys.DayOperatorAccount dgop = new Models.Sys.DayOperatorAccount();
            var query = from t in db.DayOperatorAccounts where t.Pdate.Year == _pdate.Year && t.Pdate.Month == _pdate.Month && t.Pdate.Day == _pdate.Day select t;
            foreach (Models.Sys.DayOperatorAccount o in query)
            {
                db.DayOperatorAccounts.Remove(o);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 找到某一操作员某天的所有账号
        /// </summary>
        /// <param name="_pdate"></param>
        /// <param name="operatorid"></param>
        /// <returns></returns>
        public List<Models.Sys.Account> GetDayOperatorAccounts(DateTime _pdate, int operatorid)
        {
            Models.Sys.DayOperatorAccount dgop = new Models.Sys.DayOperatorAccount();
            List<Models.Sys.Account> accounts = new List<Models.Sys.Account>();
            var query = (from t in db.DayOperatorAccounts where t.Pdate.Year == _pdate.Year && t.Pdate.Month == _pdate.Month && t.Pdate.Day == _pdate.Day && t.OperatorId == operatorid select t).ToList();
            foreach (Models.Sys.DayOperatorAccount o in query)
            {
                Models.Sys.Account id = o.Account;
                accounts.Add(id);
            }
            return accounts;
        }
    }
}
